package risoe.syslab.fhnew.hw.knx;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import risoe.syslab.fhnew.common.SystemConstants.DeviceType;
import risoe.syslab.fhnew.config.AbstractIOConfig;
import risoe.syslab.fhnew.hw.KNXInterface;
import risoe.syslab.fhnew.hw.knx.devicetypes.AnalogMeasurement;
import risoe.syslab.fhnew.hw.knx.devicetypes.BinaryActuator;
import risoe.syslab.fhnew.hw.knx.devicetypes.BinarySensor;
import risoe.syslab.fhnew.hw.knx.devicetypes.DecimalActuator;
import tuwien.auto.calimero.dptxlator.DPTXlator;
import tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat;
import tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat;
import tuwien.auto.calimero.dptxlator.DPTXlator8BitUnsigned;
import tuwien.auto.calimero.dptxlator.DPTXlatorBoolean;

/**
 * Class containing constants for use by KNX related classes
 * @author M. Svendsen
 */
public class KNXConstants {
	public static final int KNX_MAX_RETRIES = 5;
	public static final Map<DeviceType, DPTId> DPTMap;
	static{
		HashMap<DeviceType, DPTId> map = new HashMap<DeviceType, DPTId>(DeviceType.values().length);
		map.put(DeviceType.AnalogMeasurement, DPTId.ActivePower); //FIXME THIS IS NOT THE RIGHT TYPE!
		
		map.put(DeviceType.Brightness, DPTId.Brightness);
		map.put(DeviceType.DimmableLight, DPTId.Percent);
		map.put(DeviceType.PowerControlledHeater, DPTId.Percent);
		map.put(DeviceType.Door, DPTId.Switch);
		map.put(DeviceType.Window, DPTId.Switch);
		map.put(DeviceType.Presence, DPTId.Switch);
		map.put(DeviceType.Motion, DPTId.Switch);
		map.put(DeviceType.Waterboiler, DPTId.Switch);
		map.put(DeviceType.Switch, DPTId.Switch);
		map.put(DeviceType.CO2, DPTId.CO2);
		map.put(DeviceType.Temperature, DPTId.Temp);
		map.put(DeviceType.Humidity, DPTId.Hum);
		map.put(DeviceType.Voltage, DPTId.Voltage);
		map.put(DeviceType.Current, DPTId.Current);
		map.put(DeviceType.Power, DPTId.ActivePower);
		DPTMap = Collections.unmodifiableMap(map);
	}
	
	public static final Map<DeviceType, Constructor<?>> deviceConstructorMap;
	static{
		HashMap<DeviceType, Constructor<?>> amap = new HashMap<DeviceType, Constructor<?>>();
		try {
			Class<?>[] parameters = new Class<?>[] {String.class, AbstractIOConfig.class, KNXInterface.class};
			amap.put(DeviceType.AnalogMeasurement, AnalogMeasurement.class.getConstructor(parameters));
			amap.put(DeviceType.Brightness, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.CO2, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.Humidity, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.Temperature, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.DimmableLight, DecimalActuator.class.getConstructor(parameters));
			amap.put(DeviceType.PowerControlledHeater, DecimalActuator.class.getConstructor(parameters));
			amap.put(DeviceType.Door, BinarySensor.class.getConstructor(parameters));
			amap.put(DeviceType.Window, BinarySensor.class.getConstructor(parameters));
			amap.put(DeviceType.Presence, BinarySensor.class.getConstructor(parameters));
			amap.put(DeviceType.Motion, BinarySensor.class.getConstructor(parameters));
			amap.put(DeviceType.Waterboiler, BinaryActuator.class.getConstructor(parameters));
			amap.put(DeviceType.Switch, BinaryActuator.class.getConstructor(parameters));
			amap.put(DeviceType.Voltage, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.Current, KNXSensor.class.getConstructor(parameters));
			amap.put(DeviceType.Power, KNXSensor.class.getConstructor(parameters));
		} catch (Exception e) {
			System.out.println("Error mapping KNX constructors: " + e.getMessage());
		}
		deviceConstructorMap = Collections.unmodifiableMap(amap);
	}
	
	public static enum DPTId{
		// Entries are given by: main number, DPTid and subclass of DPTXlator that does the actual translation
		Switch(1, "1.001", DPTXlatorBoolean.class),
		CO2(9, "9.008", DPTXlator2ByteFloat.class),
		Hum(5, "5.001", DPTXlator8BitUnsigned.class),
		Temp(9, "9.001", DPTXlator2ByteFloat.class),
		Brightness(9, "9.004", DPTXlator2ByteFloat.class),
		Percent(5, "5.004", DPTXlator8BitUnsigned.class),
		Voltage(14, "14.027", DPTXlator4ByteFloat.class),
		Current(14, "14.019", DPTXlator4ByteFloat.class),
		ActivePower(14, "14.056", DPTXlator4ByteFloat.class);
		
		public final int mMainNumber;
		public final String mId;
		public final DPTXlator mTranslator;
		
		/**
		 * Instantiates this type of DPT using reflection to construct the appropriate DPTXlator subclass
		 */
		DPTId(int mainNumber, String id, Class<?> translator){
			this.mMainNumber = mainNumber;
			this.mId = id;
			
			DPTXlator transTemp = null;
			try {
				Constructor<?> ctor = translator.getConstructor(String.class);
				transTemp = (DPTXlator) ctor.newInstance(this.mId);
			} catch (NoSuchMethodException e){
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
			mTranslator = transTemp;
		}
	}
}
